<template>
  <!-- 舆情监测 -->
  <h-modal class="event-modal" v-model.lazy="show" title="舆情监测" @submit="submit">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" size="large">
      <a-row :gutter="100">
        <a-col :span="24">
          <a-form-model-item label="第三方公司" prop="thirdId">
            <a-select mode="multiple" v-model.lazy="form.thirdIdList" placeholder="请选择第三方公司" >
              <a-select-option v-for="item in dept" :key="item.id" :value="item.id">{{ item.companyName }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="指令名称" prop="instructions">
            <a-input v-model.lazy="form.instructions" :maxLength="50" placeholder="请输入指令名称"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="指令内容" prop="content">
            <a-textarea :rows="5" v-model.lazy="form.content" :maxLength="300" placeholder="请输入指令内容"></a-textarea>
          </a-form-model-item>
        </a-col>
      </a-row>

    </a-form-model>
  </h-modal>
</template>

<script>
import moment from 'moment';
import { addInstruction, getThirdName } from '@/api/serve';
import cloneDeep from 'lodash/cloneDeep';

export default {
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    departId: {
      type: [String, Number],
      defalut: ''
    },
    id: {
      type: String,
      defalut: ''
    },
    formData: {
      type: Object,
      default: () => {}
    }
  },
  model: {
    prop: 'showModal',
    event: 'update:showModal'
  },
  watch: {
    showModal: {
      handler(val) {
        this.show = val;
        if (val) {
          getThirdName().then(res => {
            this.dept = res.data;
            // this.form.thirdIdList = res.data.map(e => e.id);
            this.$set(this.form, 'thirdIdList', res.data.map(e => e.id));
          });
        }
      },
      immediate: true
    },
    show(val) {
      this.$emit('update:showModal', val);
    },
    departId(val) {
      this.departIds = val;
    }
  },
  data() {
    return {
      moment,
      dept: [],
      thirdId: [],
      departIds: '',
      show: false,
      deptArr: [],
      form: {
        thirdIdList: []
      },
      rules: {
        thirdIdList: [{ required: true, message: '请选择' }],
        instructions: [{ required: true, message: '请输入' }],
        content: [{ required: true, message: '请输入' }]
      }
    };
  },
  created() {

  },
  methods: {
    edit(formData) {
      this.form = cloneDeep(formData);
    },
    setThirdId(e) {
      this.$set(this.form, 'thirdIdList', e);
    },
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (!this.form.thirdIdList.length) {
            this.$message.error('请选择第三方公司');
            return;
          }
          addInstruction({ ...this.form, eventId: this.id }).then(res => {
            this.$message.success(res.msg);
            this.show = false;
            this.form = {};
            this.$emit('submit', true);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
};
</script>

<style>

</style>
